package com.yanqu.road.server.manger.activity.newyear2023;

import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.newyear2023.config.FireWorkConfig;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.newyear2023.FireWorkModule;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.utils.RandomHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class FireWorkMgr extends TempMgr {

    private static RandomHelper randomHelper = new RandomHelper();

    private static Map<Integer,ActivityInfo> activityInfoMap = new ConcurrentHashMap<>();

    private static Map<Integer, FireWorkConfig> activityConfigMap = new ConcurrentHashMap<>();


    public static ActivityInfo getTypeActivity(int childType){
        for(ActivityInfo activityInfo : activityInfoMap.values()){
            if(activityInfo.getChildType() == childType){
                return activityInfo;
            }
        }
        return null;
    }

    public static int getFireWorkActivityId(){
        for(ActivityInfo activityInfo : activityInfoMap.values()){
            if(activityInfo.getChildType() == 1){
                return activityInfo.getActivityId();
            }
        }
        return 0;
    }

    public static int getRedPacketActivityId(){
        for(ActivityInfo activityInfo : activityInfoMap.values()){
            if(activityInfo.getChildType() == 2){
                return activityInfo.getActivityId();
            }
        }
        return 0;
    }

    public static boolean isActivityInShowTime(int activityId){
        if(!activityInfoMap.containsKey(activityId)){
            return false;
        }
        ActivityInfo activityInfo = activityInfoMap.get(activityId);
        return ActivityMgr.activityInShowTime(activityInfo);
    }

    public static boolean isActivityInTime(int activityId){
        if(!activityInfoMap.containsKey(activityId)){
            return false;
        }
        ActivityInfo activityInfo = activityInfoMap.get(activityId);
        return ActivityMgr.activityInTime(activityInfo);
    }

    public static int getScoreId() {
        for(ActivityInfo activityInfo : activityInfoMap.values()){
            if(activityInfo.getChildType() == 1){
                if(activityConfigMap.containsKey(activityInfo.getActivityId())){
                    FireWorkConfig config = activityConfigMap.get(activityInfo.getActivityId());
                    return config.getScoreId();
                }
            }
        }
        return GameConfig.FIREWORKS_SOCRE_ITEMID;
    }

    public static int getSpFireId() {
        for(ActivityInfo activityInfo : activityInfoMap.values()){
            if(activityInfo.getChildType() == 1){
                if(activityConfigMap.containsKey(activityInfo.getActivityId())){
                    FireWorkConfig config = activityConfigMap.get(activityInfo.getActivityId());
                    return config.getSpFireId();
                }
            }
        }
        return GameConfig.FIREWORKS_SP_FIRE_ITEMID;
    }

    public static int getNmFireId() {
        for(ActivityInfo activityInfo : activityInfoMap.values()){
            if(activityInfo.getChildType() == 1){
                if(activityConfigMap.containsKey(activityInfo.getActivityId())){
                    FireWorkConfig config = activityConfigMap.get(activityInfo.getActivityId());
                    return config.getNmFireId();
                }
            }
        }
        return GameConfig.FIREWORKS_NM_FIRE_ITEMID;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return false;
    }

    public static void reloadActivityData() {
        getLogger().info("reload firework activity start");

        // 获取活动
        Map<Integer,ActivityInfo> tmpActivityInfoMap = new ConcurrentHashMap<>();
        Map<Integer,FireWorkConfig> tmpActivityConfigMap = new ConcurrentHashMap<>();
        List<ActivityInfo> openActivityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.FireWork2023.getValue());
        if (openActivityInfoList.size() <= 0) {
            activityInfoMap.clear();
            activityConfigMap.clear();
            getLogger().info("no firework activity in show time");
            return;
        }
        for(ActivityInfo activityInfo : openActivityInfoList){
            Map<String, ActivityConfig> activityConfigMap = NormalActivityMgr.getActivityConfigMap(activityInfo.getActivityId());
            FireWorkConfig config = new FireWorkConfig();
            config.setActivityInfo(activityInfo);
            config.initConfig(activityConfigMap);
            tmpActivityInfoMap.put(activityInfo.getActivityId(),activityInfo);
            tmpActivityConfigMap.put(activityInfo.getActivityId(),config);
        }

        // 活动配置
        activityInfoMap = tmpActivityInfoMap;
        activityConfigMap = tmpActivityConfigMap;

        reloadPlayerData();
        getLogger().info("reload firework activity end");
    }


    private static void reloadPlayerData() {
        for(ActivityInfo activityInfo : activityInfoMap.values()){
            if (ActivityMgr.activityInShowTime(activityInfo)) {
                for (GamePlayer player : GamePlayerMgr.getCopyPlayerMap().values()) {
                    if (SystemOpenMgr.systemOpen(player, eSystemId.FireWork2023.getValue())) {
                        if(activityInfo.getChildType() == 1){
                            FireWorkModule module = player.getModule(FireWorkModule.class);
                            module.initUserData(activityInfo.getActivityId());
                            module.syncUserData();
                        }else if(activityInfo.getChildType() == 2){

                        }
                    }
                }
            }
        }
    }

    public static ActivityInfo getActivityInfo(int activityId) {
        return activityInfoMap.get(activityId);
    }

    public static FireWorkConfig getActivityConfigInfo(int activityId) {
        return activityConfigMap.get(activityId);
    }

}
